package io.a.origin;

import java.util.concurrent.*;

public class Sample3_Executor3 {


    // 0自定义线程池
    private final static int AVALIABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    private final static ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVALIABLE_PROCESSORS,
            AVALIABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(5),
            /*new NamedThreadFactory("ASYNC-POOL"),*/ new ThreadPoolExecutor.CallerRunsPolicy());


    public static void main(String[] args) throws InterruptedException, ExecutionException {

        // 1.开启异步单元执行任务A
        Future<?> resultA = POOL_EXECUTOR.submit(() -> doSomethingA());

        // 2.同步等待执行结果
        System.out.println(resultA.get());

        POOL_EXECUTOR.shutdown();
    }


    public static long doSomethingA() {

        long start = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + " : --- doSomethingA---");
        return end - start;
    }
}
